﻿using System;
using UniMana.Utils.LoadData;
using UniMana.Data.Quiz;
using UniMana.Utils.CusControls;
using UniMana.Utils.Control;
using UniMana.Lib.Quiz;
using DevExpress.XtraBars;
using System.Linq;
using DevExpress.XtraEditors;
using UniMana.Utils.Config;

namespace UniMana.GUI.Quiz
{
    public partial class FrmQuestionList : DevExpress.XtraEditors.XtraForm
    {

        public DBQuiz m_data = new DBQuiz();
        public bds m_bds = new bds();

        public FrmQuestionList()
        {
            InitializeComponent();
            this.LoadData();
            this.InitControlEvent();
        }


        public void LoadData()
        {
            #region [Dropdowns]
            lueQuiz_QuestionType.GetData(m_data.m_DCReadOnly.Quiz_QuestionTypes);
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(p=>p.SubjectManage==true));
            gluTraining_Subject.GetData(m_data.m_DCReadOnly.Training_Subjects,"Training_SubjectKey");

            rluQuiz_QuestionType.GetData(m_data.m_DCReadOnly.Quiz_QuestionTypes);
            rluQuiz_SubjectTopicKey.GetData(m_data.m_DCReadOnly.Quiz_SubjectTopics, "Quiz_SubjectTopicKey");
            rluQuiz_QuestionLevel.GetData(m_data.m_DCReadOnly.Quiz_QuestionLevels, "Quiz_QuestionLevelKey");
            #endregion

            //Grid
            m_bds.DataSource = typeof(Quiz_Question);
            gctQuestionList.DataSource = m_bds;
        }

        public void InitControlEvent()
        {
            this.xucBar.btnAddNew_Click += new Controls.xucBar.ButtonClickHandler(this.btnAddNew_Click);
        }

        //Click xem
        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                int _iSubjectKey = int.Parse(gluTraining_Subject.Properties.View.GetFocusedRowCellValue("Training_SubjectKey").ToString());
                int _iType = lueQuiz_QuestionType.GetIntValue();
                m_bds.DataSource = m_data.m_DCReadOnly.Quiz_Questions.GetListByKeyAndType(_iSubjectKey, _iType);
                dlg.Close();
            }
            catch { }
        }

        private void grvQuestionList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                string quizKey = grvQuestionList.GetFocusedRowCellValue("Quiz_QuestionKey").ToString();
                Quiz_QuestionInfo questionInfo = new Quiz_QuestionInfo(quizKey);
                Quiz_AnsewerTextInfo ansTextInfo = new Quiz_AnsewerTextInfo(quizKey);
                //MessageBox.Show(ansTextInfo.Properties.Answer.ToString());
                FrmQuestionDetail frmDetail = new FrmQuestionDetail();
                frmDetail.m_QuestionInfo = questionInfo;
                frmDetail.m_AswTextInfo = ansTextInfo;
                dlg.Close();
                frmDetail.ShowDialog();
            }
           catch { XtraMessageBox.Show("Không thể xem câu hỏi","Ngân hàng đề thi - Lỗi"); }
        }

        #region [Sự kiện cho XucBar]
        //Thêm mới
        private void btnAddNew_Click(object sender, ItemClickEventArgs e)
        {
            string quizKey = DateTime.Now.ToFileTime().ToString();
            Quiz_QuestionInfo questionInfo = new Quiz_QuestionInfo(quizKey);
            questionInfo.Properties.Training_SubjectKey = gluTraining_Subject.GetIntValue();
            questionInfo.Properties.Quiz_QuestionTypeKey = lueQuiz_QuestionType.GetIntValue();
            Quiz_AnsewerTextInfo ansTextInfo = new Quiz_AnsewerTextInfo(quizKey);
            //MessageBox.Show(ansTextInfo.Properties.Answer.ToString());
            FrmQuestionDetail frmDetail = new FrmQuestionDetail();
            frmDetail.m_QuestionInfo = questionInfo;
            frmDetail.m_AswTextInfo = ansTextInfo;
            
            xucBar.dlg.Close();
            frmDetail.ShowDialog();
        }
        //Xóa
        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            try
            {
                string quizKey = grvQuestionList.GetFocusedRowCellValue("Quiz_QuestionKey").ToString();
                Quiz_QuestionInfo questionInfo = new Quiz_QuestionInfo(quizKey);
                Quiz_AnsewerTextInfo ansTextInfo = new Quiz_AnsewerTextInfo(quizKey);

                //Kiểm tra xem câu hỏi có nằm trong đề nào chưa
                //Nếu có thì ko cho xóa
                var check = m_data.m_DCReadOnly.Quiz_TestDetails.Where(p => p.Quiz_QuestionKey == quizKey);
                if (check.Count() > 0)
                {
                    XtraMessageBox.Show("Câu hỏi này đã nằm trong đề thi, không thể xóa! Vui lòng liên hệ admin để xóa.", "Ngân hàng đề thi - Thông báo");
                    return;
                }
                else
                {
                    questionInfo.Delete();
                    ansTextInfo.Delete();
                }

            }
            catch { }
        }
        #endregion

        private void grvQuestionList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void FrmQuestionList_Load(object sender, EventArgs e)
        {

        }
    }
}